package doc.ic.walkmore.weka;


import weka.classifiers.Classifier;
import weka.core.Instance;
import weka.core.Instances;
import doc.ic.walkmore.toolbox.ToolBox;

public class ClassifierBuilder {
	
	Classifier mClassifier;
	Instances trainingSet = null;
	
	public ClassifierBuilder(Classifier algo) {
		// Choose classifer algo
		mClassifier = algo;
	}
	
	public ClassifierBuilder build(Instances training) throws Exception {
		trainingSet = training;
		mClassifier.buildClassifier(trainingSet);
		return this;
	}
	
	public double[] getLikelihood(Instance observation) throws Exception {
		if (trainingSet == null) 
			throw new Exception("Training set not defined");
		else {
			observation.setDataset(trainingSet);
			return mClassifier.distributionForInstance(observation);
		}
	}
	
	public String classify(Instance observation) throws Exception {
		if (trainingSet == null) 
			throw new Exception("Training set not defined");
		else {
			observation.setDataset(trainingSet);
			return ToolBox.classes[(int) mClassifier.classifyInstance(observation)];
		}
	}
}